package com.fastloan.mgt.invest.weixin.core;

import com.fastloan.mgt.invest.weixin.model.message.ImageMessage;
import com.fastloan.mgt.invest.weixin.model.message.Location;
import com.fastloan.mgt.invest.weixin.model.message.NewsMessage;
import com.fastloan.mgt.invest.weixin.model.message.TextMessage;
import com.fastloan.mgt.invest.weixin.model.message.supers.ItemOfNewsMessage;
import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * 微信
 *
 * @author rankai
 */
public class WeixinMessage {

    private static final Logger logger = LoggerFactory.getLogger(WeixinMessage.class);

    /**
     * 把微信传过来的xml转成map
     *
     * @param request
     * @return
     * @throws IOException
     */
    public static Map<String, String> xmlToMap(HttpServletRequest request)
            throws IOException {

        Map<String, String> map = new HashMap<>();
        Document doc = null;
        InputStream ins = null;
        try {
            SAXReader reader = new SAXReader();
            ins = request.getInputStream();

            doc = reader.read(ins);
        } catch (org.dom4j.DocumentException e) {
            e.printStackTrace();
        }
        if (doc != null) {
            Element root = doc.getRootElement();
            List<Element> elelist = root.elements();
            for (Element e : elelist) {
                map.put(e.getName(), e.getText());
            }
        }
        if (ins != null) {
            ins.close();
        }
        return map;

    }

    /**
     * 微信文本对象转成xml字符串返回
     *
     * @param message
     * @return
     */

    public static String textMessageToXml(TextMessage message) {
        XStream xStream = new XStream();
        xStream.alias("xml", message.getClass());
        return xStream.toXML(message);

    }

    /**
     * 微信图片对象转成xml字符串返回
     *
     * @param image
     * @return
     */
    public static String imageMessageToXml(ImageMessage image) {
        XStream xStream = new XStream();
        xStream.alias("xml", image.getClass());
        xStream.alias("image", image.getImage().getClass());
        return xStream.toXML(image);
    }

    /**
     * 将地理位置转化为xml
     *
     * @param location
     * @return
     */

    public static String locationToXml(Location location) {
        XStream xStream = new XStream();
        xStream.alias("xml", location.getClass());
        return xStream.toXML(location);
    }

    /**
     * 将微信图文消息转化为xml
     *
     * @param newsMessage
     * @return
     */

    public static String newsMessageToXml(NewsMessage newsMessage) {
        XStream xStream = new XStream();
        xStream.alias("xml", newsMessage.getClass());
        xStream.alias("item", new ItemOfNewsMessage().getClass());
        return xStream.toXML(newsMessage);
    }

    /**
     * 获取请求相关用户数据
     *
     * @param request http请求对象
     * @return 用户数据map
     */
    public static Map<String, String[]> getUserData(HttpServletRequest request, Boolean islog) {
        Map<String, String[]> map = request.getParameterMap();
        Set keySet = map.entrySet();
        if (islog) {
            logger.debug("微信请求数据开始");
            for (Iterator itr = keySet.iterator(); itr.hasNext(); ) {
                Map.Entry me = (Map.Entry) itr.next();
                Object ok = me.getKey();
                Object ov = me.getValue();
                String[] value = new String[2];
                if (ov instanceof String[]) {
                    value = (String[]) ov;
                } else {
                    value[0] = ov.toString();
                }
                logger.debug("key:" + ok);
                logger.debug("value:");
                for (int k = 0; k < value.length; k++) {
                    logger.debug(value[k]);
                }
            }
            logger.debug("微信请求数据结束");
        }
        return map;
    }
}
